package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.utils.CommonMessage;

@Controller
@RequestMapping(value = "adminapi")
public class AAdministratorController {

	Logger logger = Logger.getLogger(AUserController.class);

	// 注入一个UserService对象
	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "userMapper")
	private UserMapper UserMapper;

	/**
	 * 显示管理员列表的方法
	 * 
	 * @param page
	 * @param pageSize
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "listAdministrator", method = RequestMethod.GET)
	public String listUser(Integer ustate, Model model) {
		// PageData<User> pd = userService.getUserByPage(1, 10, content, 3);
		// List<User> list = pd.getData();
		List<User> list = userService.getusers(ustate);
		model.addAttribute("list", list);

		return "admin/administrator-list";

	}

	/**
	 * 添加管理员步骤一
	 * 
	 * @return
	 */
	@RequestMapping(value = "intoAdministratorAdd")
	public String intoMenber() {
		return "admin/administrator-add";
	}

	/**
	 * 添加管理员步骤二
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "addAdministrator", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage addUser(@RequestBody User user) {

		logger.info("addAdministrator************:" + user);

		boolean flag = userService.addUser(user);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 修改管理员步骤一
	 */
	@RequestMapping(value = "updateAdministrator01", method = RequestMethod.GET)
	public String updateUser01(int uid, String uname, String upwd, String usex, String utel, int ustate, Model model) {
		logger.info("updateAministrator01************:" + uid + uname + upwd + usex + utel + ustate);

		User user = new User(uid, uname, upwd, usex, utel, ustate);

		model.addAttribute("user", user);
		return "admin/administrator-update";
	}

	/**
	 * 修改管理员步骤二
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "updateAdministrator", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateUser(@RequestBody User user) {

		logger.info("updateAdministrator************:" + user);

		boolean flag = userService.updateUser(user);

		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 删除管理员
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "deleteAdministrator", method = RequestMethod.GET)
	@ResponseBody
	public CommonMessage deleteUser(int uid, HttpSession session) {
		logger.info("deleteAdministrator01************:" + uid);

		User user = (User) session.getAttribute("user");

		logger.info("session:" + user.getUid());

		boolean flag = false;
		if (user.getUid() == uid) {
			flag = false;
		} else {
			flag = userService.deleteUserById(uid, 0);
		}
		CommonMessage cm = new CommonMessage();
		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;

	}

}
